package com.yonyou.minio.demo;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.*;

/**
 * @Author wwj
 * @Date 2021/12/14
 * @Description 文件上传
 */
public class FileUpload {

    public static void main(String[] args) throws Exception{

        try {
            // 1 创建mc
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://10.6.174.190:9000")
                    .credentials("minioadmin", "minioadmin")
                    .build();
            // 2 创建bucket
            String bucketName = "bucket01";
            // 3 判断
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!found) {
                // 不存在，则创建bucket
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
            // 4 上传文件
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucketName)
                            .object("test.jpg")
                            .filename("D:/test.jpg")
                            .build());

            System.out.println("上传文件成功！");
        } catch (MinioException e) {
            System.out.println("Error occurred :" + e);
            System.out.println("HTTP trace :" + e.httpTrace());
        }
    }
}
